package genericdemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏西西
 * Date: 2023-10-27
 * Time: 15:02
 */
class MyArray1<T>{
    public T[] obj = (T[])new Object[3];//讲数组定义为Object数组，长度为3，不放数据

    //提供一个方法，getPos，给一个pos下标，就可以返回pos下标对应的值。
    public T getPos(int pos){
        return obj[pos];
    }

    public void setObj(int pos,T val){
        obj[pos] = val;
    }
}
public class Test2 {

    public static void main(String[] args) {
        //实例化对象的同时 指定当前泛型类 的指定参数类型是Integer
        MyArray1<Integer> myArray1 = new MyArray1<>();
        myArray1.setObj(0,10);
        myArray1.setObj(2,60);
        myArray1.setObj(3,1000);

        double d = (double)myArray1.getPos(3);

        System.out.println("======================");

        MyArray1<String> myArray2 = new MyArray1<>();
        myArray2.setObj(0,"susu");
        myArray2.setObj(1,"xixi");
    }
}
